﻿// ParseLib - An open source library for natural language processing.
// Copyright (C) Kais Dukes, 2012. E-mail: kais@kaisdukes.com
// http://parselib.org

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ParseLib.Corpora
{
    public class TextualUnit : IEnumerable<IToken>
    {
        private readonly ICorpus _corpus;
        private readonly List<IToken> _tokens;

        public TextualUnit(ICorpus corpus, IEnumerable<IToken> tokens = null)
        {
            _corpus = corpus;
            _tokens = tokens != null ? new List<IToken>(tokens) : new List<IToken>();
        }

        public ICorpus Corpus
        {
            get { return _corpus; }
        }

        public int TokenCount
        {
            get { return _tokens.Count; }
        }

        public IToken this[int index]
        {
            get { return _tokens[index]; }
        }

        public void AddToken(IToken token)
        {
            _tokens.Add(token);
        }

        public TextualUnit CopyText()
        {
            var size = _tokens.Count;
            var copy = new IToken[size];
            for (var i = 0; i < size; i++)
            {
                copy[i] = _tokens[i].CopyText();
            }
            return new TextualUnit(_corpus, copy);
        }

        public override string ToString()
        {
            var text = new StringBuilder();
            foreach(var token in _tokens)
            {
                if (text.Length > 0)
                {
                    text.AppendLine();
                }
                text.Append(token);
            }
            return text.ToString();
        }

        public IEnumerator<IToken> GetEnumerator()
        {
            return _tokens.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
